/**
@author Natalia Bazhenova 2005-2007
*/

var inps
var f
setSkin()	
window.onresize=setSkin

function setSkin() {
	fcontainer=document.getElementById('formcontainer')
	if(!fcontainer) {
		alert("Error: form container not defined!"); return false
	}
	//get all forms in this container
	fs=fcontainer.getElementsByTagName('FORM')
	for (ii=0;ii<fs.length;ii++) {
		// one form
		f=fs[ii]
		inps=f.getElementsByTagName("INPUT")
		imgs=f.getElementsByTagName("IMG")
		for(i=0;i<imgs.length;i++) { // remove all this imgs if resize
			if(imgs[i].getAttribute("prettyCheckbox")=="1") { 	
				//there is a bug in Opera 9: by resizing, the images stays partially appear
				imgs[i].parentNode.removeChild(imgs[i]);
				i--;
			}
		}
		// for each input in this form:
		for (i=0;i<inps.length;i++) {
			inp=inps[i]
			type=inp.getAttribute("type").toLowerCase()
			if ((type!="radio") && (type!="checkbox")) 
				continue
			im=document.createElement("IMG");	
			im.setAttribute("prettyCheckbox","1");
			if (type=="radio") 
				im.setAttribute("name", inps[i].getAttribute("name"))
			chk=isChecked(inp)
			s=type+(chk ? 1 : 0)+".gif"; 
			im.setAttribute("src","skins/"+skin+s)	
			parElt=inp.parentNode
			setPosition(inp,im)	
			parElt.appendChild(im);
			inp.style.visibility="hidden"
			im.onmouseover=function(){
				this.src=this.src.replace(".gif","_over.gif")
			}
			im.onmouseout=function(){
				this.src=this.src.replace("_over.gif",".gif")
			}
			eval("im.onclick=function(){ setCheck("+i+",'"+type+"',this,"+ii+")}")
		}
	}
}

function setCheck(j,t,obj,ii) {
	if (t=="radio") { //uncheck all this elts
		n=f.elements[j].getAttribute("name")
		f=document.forms[ii]
		inps=f.getElementsByTagName("INPUT")
		for(i=0;i<inps.length;i++) {
			if(inps[i].getAttribute("name")==n) 
				inps[i].removeAttribute("checked")
		}
		//replace all the image src's
		imgs=f.getElementsByTagName("IMG")
		for(i=0;i<imgs.length;i++) {
			if(imgs[i].getAttribute("name")==n) { 		
				imgs[i].src=imgs[i].src.replace("1.gif","0.gif")
			}
		}
		f.elements[j].setAttribute('checked','checked');
		obj.src=obj.src.replace("0_over.gif","1_over.gif")
	}
	if (t=="checkbox") { 
		 chk=isChecked(f.elements[j])
		 if (!chk) {
		 	f.elements[j].checked=true;// IE
			f.elements[j].setAttribute('checked','checked');
			obj.src=obj.src.replace("0_over.gif","1_over.gif")
		}
		else {
		 		f.elements[j].checked=false // IE
				b=f.elements[j].removeAttributeNode(f.elements[j].getAttributeNode("CHECKED"));//Mozilla
				obj.src=obj.src.replace("1_over.gif","0_over.gif")
		}
	}
}

function isChecked (elt){
	c=elt.getAttributeNode("checked");
	try {
		if (c) 
			c.value="checked" 
	} catch(e){alert(e.errorText)} //for Mozilla
	chk=elt.getAttribute("checked"); 
	if(chk=="false") 
		chk=false; //for Opera
	return chk
}

function setPosition(elt,anotherElt) {
	pos2=findPos(elt); 
	anotherElt.style.position="absolute"
	// IE bug
	topM=0;leftM=0;
	if (document.body.topMargin)	topM=document.body.topMargin;
	if(document.body.leftMargin)	leftM=document.body.leftMargin;//alert(leftM)
	//
	anotherElt.style.left=(parseInt(pos2[0])+parseInt(leftM/2))+"px";
	anotherElt.style.top=(parseInt(pos2[1])+parseInt(topM/4))+"px"
}

/** Source: http://www.quirksmode.org/js/findpos.html - is better than my own**/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	} 
	return [curleft,curtop];
} 

